package com.dhsc.service.impl.ywjk.qywjh.jhlxwh;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.jkzx.ywjk.qywjh.JsptSystemCzmxVo;
import com.dhsc.domain.jkzx.ywjk.qywjh.JsptSystemJyrVo;
import com.dhsc.domain.jkzx.ywjk.qywjh.jhlxwh.TjkYclxRyglVo;
import com.dhsc.domain.jkzx.ywjk.qywjh.jhlxwh.TjkYclxSqlVo;
import com.dhsc.domain.jkzx.ywjk.qywjh.jhmxxx.TjkYclxVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywjk.qywjh.jhlxwh.IJhlxcsDao;
import com.dhsc.service.ywjk.qywjh.jhlxwh.IJhlxcsService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: JhlxcsServiceImpl
 * @Description: 稽核类型参数
 * @Author: jwj
 * @Date: 2023/11/7 11:06
 */
@Service
@Slf4j
public class JhlxcsServiceImpl implements IJhlxcsService {

    @Autowired
    private IJhlxcsDao jhlxcsDao;


    @Override
    public LayuiTableVo queryJhlxcs(TjkYclxVo tjkYclxVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            //不是导出时才分页
            if (tjkYclxVo.getExportFlag() != 1) {
                PageMethod.startPage(tjkYclxVo.getPage(), tjkYclxVo.getLimit());
            }
            List<TjkYclxVo> list = jhlxcsDao.queryJhlxcs(tjkYclxVo);
            PageInfo<TjkYclxVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        } catch (Exception e) {
            log.error("查询稽核类型参数信息出错", e);
            throw new MyException("查询稽核类型参数信息出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject jhlxcsgx(TjkYclxVo tjkYclxVo) {
        ResponseObject obj = new ResponseObject();
        try {
            //新增
            if (tjkYclxVo.getOperateType() == 0) {
                //查询异常类型或名称是否已存在
                int count = jhlxcsDao.queryYclxByYcmcAndYclx(tjkYclxVo);
                if (count > 0) {
                    obj.setStatus(400);
                    obj.setMessage("已存在相同异常类型或名称的记录！");
                    return obj;
                }
                jhlxcsDao.saveYclx(tjkYclxVo);
                //监控信息修改
            } else if (tjkYclxVo.getOperateType() == 1) {
                //查询异常类型或名称是否已存在
                int count = jhlxcsDao.queryYclxByYcmcAndYclx(tjkYclxVo);
                if (count > 0) {
                    obj.setStatus(400);
                    obj.setMessage("已存在相同异常类型或名称的记录！");
                    return obj;
                }
                jhlxcsDao.updateYclx(tjkYclxVo);
                //监控状态修改
            } else if (tjkYclxVo.getOperateType() == 2) {
                //更新异常类型激活状态
                jhlxcsDao.updateYclx(tjkYclxVo);
                //根据CID查询异常类型信息
                TjkYclxVo queryYclx = jhlxcsDao.queryYclxBycid(tjkYclxVo.getCid());
                JsptSystemCzmxVo jsptSystemCzmxVo = new JsptSystemCzmxVo();
                jsptSystemCzmxVo.setUsername(tjkYclxVo.getUsername());
                String zy = queryYclx.getYclx() + "-" + queryYclx.getYcmc();
                if ("0".equals(tjkYclxVo.getJhbz())) {
                    zy = zy + "状态置为不激活。";
                } else if ("1".equals(tjkYclxVo.getJhbz())) {
                    zy = zy + "状态置为激活。";
                }
                zy = zy + "原因：" + tjkYclxVo.getXgyy();
                jsptSystemCzmxVo.setZy(zy);
                //新增激活状态修改记录
                jhlxcsDao.insertJsptSystemCzmx(jsptSystemCzmxVo);
            }
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("更新稽核类型参数出错", e);
            throw new MyException("更新稽核类型参数出错", e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo queryRyxxByWbbh(TjkYclxRyglVo tjkYclxRyglVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(tjkYclxRyglVo.getPage(), tjkYclxRyglVo.getLimit());
            List<TjkYclxRyglVo> list = jhlxcsDao.queryRyxxByWbbh(tjkYclxRyglVo.getWbbh());
            PageInfo<TjkYclxRyglVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        } catch (Exception e) {
            log.error("根据wbbh查询人员信息表格出错", e);
            throw new MyException("根据wbbh查询人员信息表格出错", e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryRyxzInfo() {
        ResponseObject obj = new ResponseObject();
        try {
            List<TjkYclxRyglVo> tjkYclxRyglVos = jhlxcsDao.queryRyxzInfo();
            obj.setData(tjkYclxRyglVos);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("人员选择下拉查询出错", e);
            throw new MyException("人员选择下拉查询出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject addRyxx(TjkYclxRyglVo tjkYclxRyglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            if (!CommonUtil.isNotEmptyObj(tjkYclxRyglVo.getWbbh())) {
                obj.setStatus(500);
                obj.setMessage("保存失败：wbbh不能为空！");
                return obj;
            }
            if (!CommonUtil.isNotEmptyObj(tjkYclxRyglVo.getBadge())) {
                obj.setStatus(500);
                obj.setMessage("保存失败：工号不能为空！");
                return obj;
            }
            if (!CommonUtil.isNotEmptyObj(tjkYclxRyglVo.getRylx())) {
                obj.setStatus(500);
                obj.setMessage("保存失败：人员类型不能为空！");
                return obj;
            }
            jhlxcsDao.delRyxx(tjkYclxRyglVo);
            List<TjkYclxRyglVo> tjkYclxRyglVos = new ArrayList<>();
            List<String> badgeList = Arrays.asList(tjkYclxRyglVo.getBadge().split(","));
            for (int i = 0; i < badgeList.size(); i++) {
                TjkYclxRyglVo ryxx = new TjkYclxRyglVo();
                ryxx.setWbbh(tjkYclxRyglVo.getWbbh());
                ryxx.setRylx(tjkYclxRyglVo.getRylx());
                ryxx.setBadge(badgeList.get(i));
                ryxx.setUsername(tjkYclxRyglVo.getUsername());
                tjkYclxRyglVos.add(ryxx);
            }
            jhlxcsDao.insertRyxx(tjkYclxRyglVos);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("新增人员信息出错", e);
            throw new MyException("新增人员信息出错", e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateRyxx(TjkYclxRyglVo tjkYclxRyglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            int count = jhlxcsDao.queryRyxxByBadgeAndRylx(tjkYclxRyglVo);
            if (count > 0) {
                obj.setStatus(400);
                obj.setMessage("已存在相同工号和人员类型的记录！");
                return obj;
            }
            jhlxcsDao.updateRyxx(tjkYclxRyglVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("修改人员信息出错", e);
            throw new MyException("修改人员信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject delRyxx(TjkYclxRyglVo tjkYclxRyglVo) {
        ResponseObject obj = new ResponseObject();
        try {
            if (!CommonUtil.isNotEmptyObj(tjkYclxRyglVo.getBh())) {
                obj.setStatus(500);
                obj.setMessage("删除失败：编号不能为空！");
                return obj;
            }
            jhlxcsDao.delRyxx(tjkYclxRyglVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除人员信息出错", e);
            throw new MyException("删除人员信息出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo querySqlxxByWbbh(TjkYclxSqlVo tjkYclxSqlVo) {
        LayuiTableVo obj = new LayuiTableVo();
        try {
            PageMethod.startPage(tjkYclxSqlVo.getPage(), tjkYclxSqlVo.getLimit());
            List<TjkYclxSqlVo> list = jhlxcsDao.querySqlxxByWbbh(tjkYclxSqlVo.getWbbh());
            List<String> jyr=getLastDay((Integer.parseInt(CommonUtil.getDateSystem().substring(0,4))+1)+CommonUtil.getDateSystem().substring(4),600);
            for (TjkYclxSqlVo yclxSqlxx : list) {
                String sqlfyh=CommonUtil.getTransString(yclxSqlxx.getSqlexe(),jyr,CommonUtil.getDateSystem());
                yclxSqlxx.setSqlfyh(sqlfyh);
            }
            PageInfo<TjkYclxSqlVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        } catch (Exception e) {
            log.error("根据wbbh查询SQL信息出错", e);
            throw new MyException("根据wbbh查询SQL信息出错", e);
        }
        return obj;
    }

    @Override
    public ResponseObject addSqlxx(TjkYclxSqlVo tjkYclxSqlVo) {
        ResponseObject obj = new ResponseObject();
        try {
            int count = jhlxcsDao.querySqlxxExistByXhAndWbbh(tjkYclxSqlVo);
            if (count > 0) {
                obj.setStatus(500);
                obj.setMessage("更新失败：存在序号相同的记录！");
                return obj;
            }
            jhlxcsDao.addSqlxx(tjkYclxSqlVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("新增SQL信息出错", e);
            throw new MyException("新增SQL信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateSqlxx(TjkYclxSqlVo tjkYclxSqlVo) {
        ResponseObject obj = new ResponseObject();
        try {
            if (!CommonUtil.isNotEmptyObj(tjkYclxSqlVo.getBh())) {
                obj.setStatus(500);
                obj.setMessage("更新失败：编号不能为空！");
                return obj;
            }
            int count = jhlxcsDao.querySqlxxExistByXhAndWbbh(tjkYclxSqlVo);
            if (count > 0) {
                obj.setStatus(500);
                obj.setMessage("更新失败：存在序号相同的记录！");
                return obj;
            }
            jhlxcsDao.updateSqlxx(tjkYclxSqlVo);
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("修改SQL信息出错", e);
            throw new MyException("修改SQL信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject delSqlxx(TjkYclxSqlVo tjkYclxSqlVo) {
        ResponseObject obj = new ResponseObject();
        try {
            if (!CommonUtil.isNotEmptyObj(tjkYclxSqlVo.getBh())) {
                obj.setStatus(500);
                obj.setMessage("删除失败：编号不能为空！");
                return obj;
            }
            jhlxcsDao.delSqlxx(tjkYclxSqlVo.getBh());
            obj.setStatus(200);
        } catch (Exception e) {
            log.error("删除SQL信息出错", e);
            throw new MyException("删除SQL信息出错",e);
        }
        return obj;
    }

    public List<String> getLastDay(String date,int sl){
        List<String> rqList=new ArrayList<>();
        List<JsptSystemJyrVo> jsptSystemJyrVos=jhlxcsDao.queryJyrBeforeRq(date);
        if(CommonUtil.isNotEmptyList(jsptSystemJyrVos)){
            for(int i=0;i<jsptSystemJyrVos.size();i++){
                if(i==sl-1){
                    return rqList;
                }else{
                    String rq=jsptSystemJyrVos.get(i).getRq();
                    rqList.add(rq);
                }
            }
        }
        return rqList;
    }

}

